package com.blankii.androidlibrary.utils

import android.view.View

/**
 * author: blankii
 * created on: 2024/11/26 13:09
 * description:
 */
object ViewClickContinuousUtil {
    private var rootClickCount = 1
    private var lastTime = 0L

    private var lastClickTime: Long = 0

    /**
     * 绑定view 设置连续点击事件处理
     */
    fun setViewConfig(
        view: View, maxCount: Int = 2, maxSeconds: Int = 2, callBack: ClickContinuousCallback
    ) {
        view.setOnClickListener {
            if (System.currentTimeMillis() - lastClickTime < maxSeconds * 1000) {
                rootClickCount++
                if (rootClickCount >= maxCount) {
                    callBack.success()
                    rootClickCount = 0
                    callBack.currentCount(rootClickCount)
                    return@setOnClickListener
                }
            } else {
                rootClickCount = 0
            }
            callBack.currentCount(rootClickCount)
            lastClickTime = System.currentTimeMillis()
        }
    }


    /**
     * 2秒内连续点击退出应用
     */
    private var firstTime = 0L
    fun backClickConfig(callBack: BackClickCallback) {
        if (System.currentTimeMillis() - firstTime < 2000) {
            callBack.success()
        } else {
            firstTime = System.currentTimeMillis()
            callBack.clickOne()
        }
    }

    interface ClickContinuousCallback {
        fun success()
        fun currentCount(count: Int)
    }

    interface BackClickCallback {
        fun success()
        fun clickOne()
    }
}